package com.hzlx.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class P02 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 线程数组
        List<CompletableFuture<Long>> futures = new ArrayList<>();
        for(int i=0;i<10;i++){
            CompletableFuture f1 = CompletableFuture.supplyAsync(()->{
                try {
                    Thread.sleep(Math.round(Math.random() * 1000));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                long count =  Math.round(Math.random()*10);
                System.out.println(Thread.currentThread().getName()+"抓到了" + count+"个知了猴");
                return count;
            });
            futures.add(f1);
        }
        Long total = 0L;
        for(CompletableFuture<Long> future : futures){
            total += future.get();
        }
        System.out.println("总共抓了：" + total);
    }
}
